package com.liesp.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
* 项目名称：Liesp<br>
* 类名称：ListUtil <br>  
* 类描述：  List处理类 <br>
* 创建人：许彪   
* 创建时间：2009-8-28 上午11:37:31 <br> 
* 修改人：   
* 修改时间：                  <br>  
* 修改备注：   
* @version V1.0
 */
public class ListUtil {
	private static Logger log = LoggerFactory.getLogger(ListUtil.class);

	/**
	 * List是否为空的验证
	 * 
	 * @param parm
	 * @return
	 */
	public static boolean isNullOrEmpty(List parm) {
		boolean rtn = false;
		if (null == parm || parm.size() == 0) {
			rtn = true;
		}
		return rtn;
	}
	/**
	 * 查询o在dataList中的位置.从0开始
	 * 
	 * @param dataList
	 *            数据列表的list
	 * @param o
	 *            目标对象
	 * @param idFieldName
	 *            主码的字段名称 如果为空,则直接使用对象引用进行比较
	 * @return int >=0：位置 -1:未找到
	 */
	public static int getPosFromList(List dataList, Object o, String idFieldName) {
		int r = -1;
		if (dataList == null || dataList.isEmpty() || o == null) {
			return r;
		}
		if (idFieldName == null) {// 如果没有指定主码,则自动比较两个对象的引用
			for (int i = 0; i < dataList.size(); i++) {
				if (dataList.get(i) == o) {
					r = i;
					break;
				}
			}
		} else {
			try {
				Method idMethod = o.getClass().getMethod(
						"get" + StringUtil.setFirstCharUpcase(idFieldName));
				Object key = idMethod.invoke(o, null);
				Object data = null;
				for (int i = 0; i < dataList.size(); i++) {
					data = dataList.get(i);
					if (data != null) {
						if (key.equals(idMethod.invoke(data, null))) {
							r = i;
						}
					}
				}

			} catch (Exception e) {
				// 在方法名称不对的时候,系统在后台进行提示
				log.error("May idFieldName is wrong! idFieldName:"
						+ idFieldName + ",className:" + o.getClass().getName());
				e.printStackTrace();
			}
		}
		return r;
	}

	/**
	 * 查询o在dataList中的位置.从0开始
	 * 
	 * @param dataList
	 *            数据列表的list
	 * @param o
	 *            idValue 目标对象的主键
	 * @param idFieldName
	 *            主码的字段名称 如果为空,则直接使用对象引用进行比较
	 * @return int >=0：位置 -1:未找到
	 */
	public static int getPosFromListIdValue(List dataList, Object idValue,
			String idFieldName) {
		int r = -1;
		if (dataList == null || dataList.isEmpty() || idValue == null) {
			return r;
		}
		try {
			Method idMethod = dataList.get(0).getClass().getMethod(
					"get" + StringUtil.setFirstCharUpcase(idFieldName));
			Object value = null;
			int pos = 0;
			for (Object obj : dataList) {
				value = idMethod.invoke(obj, null);
				if (value.toString().equals(idValue.toString())) {
					r = pos;
					break;
				}
				pos++;
			}
		} catch (Exception e) {
			// 在方法名称不对的时候,系统在后台进行提示
			log.error("May idFieldName is wrong! idFieldName:" + idFieldName);
		}
		return r;
	}

	/**
	 * 排除list中重复的记录
	 * 
	 * @param list
	 * @return
	 */
	public static List uniqueList(List list) {
		if (isNullOrEmpty(list)) {
			return list;
		}
		Map map = new HashMap(list.size());
		for (Object o : list) {
			map.put(o, "1");
		}
		List r = list;
		if (map.size() != list.size()) {
			r = new ArrayList(map.size());
			for (Object o : list) {
				if (map.get(o) != null) {
					r.add(o);
					map.remove(o);
				}
			}
		}
		map = null;
		list = null;
		return r;

	}

	/**
	 * 排除list中重复的记录
	 * 
	 * @param list
	 * @return
	 */
	public static List uniqueList(List list, String idFieldName) {
		if (isNullOrEmpty(list)) {
			return list;
		}
		if (StringUtil.isEmpty(idFieldName)) {
			return uniqueList(list);
		}
		List r = list;
		try {
			Method idMethod = list.get(0).getClass().getMethod(
					"get" + StringUtil.setFirstCharUpcase(idFieldName));
			Object value = null;
			Map map = new HashMap(list.size());
			for (Object o : list) {
				map.put(idMethod.invoke(o, null), "1");
			}
			if (map.size() != list.size()) {
				r = new ArrayList(map.size());
				for (Object o : list) {
					value = idMethod.invoke(o, null);
					if (map.get(value) != null) {
						r.add(o);
						map.remove(value);
					}
				}
			}
			map = null;
			list = null;
		} catch (Exception e) {
			// 在方法名称不对的时候,系统在后台进行提示
			log.error("May idFieldName is wrong! idFieldName:" + idFieldName);
			e.printStackTrace();
		}
		return r;

	}

	public static List getOneFieldValue(List list, String fieldName) {
		if (isNullOrEmpty(list)) {
			return new ArrayList();
		}
		if (StringUtil.isEmpty(fieldName)) {
			return list;
		}
		List r = new ArrayList(list.size());
		try {
			Method method = list.get(0).getClass().getMethod(
					"get" + StringUtil.setFirstCharUpcase(fieldName));
			Object value = null;
			for (Object o : list) {
				r.add(method.invoke(o, null));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return r;
	}

	/**
	 * 将list按照指定长度分割为多个list
	 * 
	 * @param <T>
	 * @param list
	 * @param subListSize
	 * @return
	 */
	public static <T> List<List<T>> splitList(List<T> list, int subListSize) {
		List<List<T>> r = new ArrayList<List<T>>();
		if (isNullOrEmpty(list) || subListSize < 0) {
			r.add(list);
			return r;
		}
		int maxSize = subListSize;
		int subNum = list.size() / maxSize + 1;
		int endIndex = 0;
		for (int i = 0; i < subNum; i++) {
			if (i * maxSize < list.size()) {
				List<T> subList = new ArrayList<T>(maxSize);
				endIndex = (i + 1) * maxSize;
				endIndex = endIndex < list.size() ? endIndex : (list.size());
				subList.addAll(list.subList(i * maxSize, endIndex));
				r.add(subList);
			}
		}

		return r;
	}
}
